﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;

namespace Roovent.HttpOperator
{
	public sealed class AttributeCollection : StringDictionary //MSDN:键的处理方式是不区分大小写的
	{
		#region 构造函数

		public AttributeCollection()
		{
		}

		#endregion


		#region 公共方法

		public override void Add(string key, string value)
		{
			base.Add(key, value.ToLowerInvariant());
		}

		public void AddAttributes(string keyValueList) //形如:key1="value1" key2="value2" ... keyN="valueN"
		{
			Regex regex = new Regex(
				"\\b(?<key>\\w+)\\b\\s*=\\s*(([\"\'](?<value>.+?)[\"\'])|(?<value>[^<>=\\s]+))",
				RegexOptions.IgnoreCase);
			MatchCollection mc = regex.Matches(keyValueList);
			foreach (Match m in mc)
			{
				this.Add(m.Groups["key"].Value, m.Groups["value"].Value);
			}
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			foreach (string key in base.Keys)
			{
				sb.Append(string.Format("{0}=\"{1}\" ", key, base[key]));  //key1="value1"
			}
			return sb.ToString().Trim();
		}

		#endregion
	}

	public sealed class HtmlLabelCollection : CollectionBase
	{
		#region 构造函数

		public HtmlLabelCollection()
		{
		}

		public HtmlLabelCollection(HtmlLabel[] labels)
		{
			this.AddRange(labels);
		}

		public HtmlLabelCollection(int capacity) : base(capacity)
		{
		}

		#endregion


		#region 公共函数

		public int Add(HtmlLabel label)
		{
			return base.List.Add(label);
		}

		public void AddRange(HtmlLabel[] labels)
		{
			if(labels == null)
				throw new ArgumentNullException("labels is null");

			for (int i = 0; i < labels.Length; ++i)
			{
				this.Add(labels[i]);
			}
		}

		//不能重写Clear()方法

		public bool Contains(HtmlLabel label)
		{
			return base.List.Contains(label);
		}

		public void CopyTo(HtmlLabel[] labels, int index) //从index开始复制
		{
			base.List.CopyTo(labels, index);
		}

		public int IndexOf(HtmlLabel label)
		{
			return base.List.IndexOf(label);
		}

		public void Insert(int index, HtmlLabel label)
		{
			base.List.Insert(index, label);
		}

		public void Remove(HtmlLabel label)
		{
			base.List.Remove(label);
		}

		//不能重写RemovAt()方法

		#endregion


		#region 公共属性

		public HtmlLabel this[string name]
		{
			get
			{
				int i = IndexOfLabel(name);
				if (i < 0)
					return null;
				else
					return (HtmlLabel)base.List[i];
			}
		}

		public HtmlLabel this[int index]
		{
			get { return (HtmlLabel)base.List[index]; }
			set { base.List[index] = value; }
		}

		#endregion


		#region 辅助函数

		private int IndexOfLabel(string name) //查找Name属性为name的InputLabel的索引
		{
			for (int i = 0; i < this.Count; ++i)
			{
				HtmlLabel one = (HtmlLabel)base.List[i];
				if (string.Compare(one.ID, name, StringComparison.OrdinalIgnoreCase) == 0
					|| (string.Compare(one.Name, name, StringComparison.OrdinalIgnoreCase) == 0))
				{
					return i;
				}
			}
			return -1;
		}

		#endregion
	}
}